<?php

namespace Modules\Asset\Entities;

use \Modules\Chip\Entities\Chip;

use ThrowException;

use Setting;

// 常规类资产 (如积分、红包) 
class AllAsset
{
    public $assets;

    public function __construct($assets)
    {
        $this->assets = $assets;
    }

    // 获取碎片
    public function getChip()
    {
        $chip = null;

        if (@$this->assets['is_chip'] && @$this->assets['chip_id']) {
            $chip = Chip::getEntity([
                'id' => @$this->assets['chip_id'] ?: 0
            ], false);
        }

        return $chip;
    }

    public function getInfo()
    {
        $assets = $this->assets;

        $chip = $this->getChip();
        if ($chip) {
            $assets['chip_title'] = $chip->title;
        }

        return $assets;
    }

    // 碎片数量
    public function getChipTotal()
    {
        return $this->assets['chip_total'] ?: 0;
    }

    // 发送给用户
    public function sendTo($user, $options)
    {
        $assetArray = $this->assets;

        // 计算应得的基础奖励
        $score = null;
        $redpack = null;
        $levelScore = null;
        $showBoxCard = null;    // 显示卡
        $excludeBoxCard = null;  // 排除卡

        $description = @$options['description'];
        $sendType = @$options['type'];

        $order = @$options['order'];

        $res = [];

        // 有设置积分奖励
        if (@$assetArray['is_score']) {
            $score = @$assetArray['score'] ?: 0;
        }

        // 有设置积分奖励
        if (@$assetArray['is_redpack']) {
            $redpack = @$assetArray['redpack'] ?: 0;
        }

        // 有设置积分奖励
        if (@$assetArray['is_level_score']) {
            $levelScore = @$assetArray['level_score'] ?: 0;
        }

        // 有设置显示卡
        if (@$assetArray['is_show_box_card']) {
            $showBoxCard = @$assetArray['show_box_card'] ?: 0;
        }

        // 有设置显示卡
        if (@$assetArray['is_exclude_box_card']) {
            $excludeBoxCard = @$assetArray['exclude_box_card'] ?: 0;
        }

        // 发放积分
        if ($score) {
            $user->asset('score')->in($score, $options);
            $res['score'] = [
                'total' => $score
            ];
        }

        // 发放红包
        if ($redpack) {
            $user->asset('redpack')->in($redpack, $options);
            $res['redpack'] = [
                'total' => $redpack
            ];
        }

        // 发放成长值
        if ($levelScore) {
            $user->asset('level_score')->in($levelScore, $options);
            $res['level_score'] = [
                'total' => $levelScore
            ];
        }

        // 发放显示卡
        if ($showBoxCard) {
            // $card = new Card('show_box');
            $user->card_asset('show_box')->in($showBoxCard, $options);
            $res['show_box_card'] = [
                'total' => $showBoxCard
            ];
        }

        // 发放排除卡
        if ($excludeBoxCard) {
            // $card = new Card('show_box');
            $user->card_asset('exclude_box')->in($excludeBoxCard, $options);
            $res['exclude_box_card'] = [
                'total' => $excludeBoxCard
            ];
        }

        // 发放碎片
        $chip = $this->getChip();
        if ($chip) {
            $chipTotal = $this->getChipTotal();
            if ($chipTotal) {
                // \Log::error($options);
                $user->chipAsset($chip->id)->in($chipTotal, $options);
            }
            $res['chip'] = [
                'total' => $chipTotal,
                'chip_id' => $chip->id,
                'chip_title' => $chip->title
            ];
        }

        return $res;
    }
}
